package com.pengtu.entity.project;

import java.util.Comparator;
import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.Where;

import com.pengtu.entity.IdEntity;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.app.Organization;

@Entity
@Table(name = "T_PROJ_QUALIFICATION")
public class Qualification extends IdEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Organization organization;
	private String grade;
	private String code;
	private String certificate;
	private Date certificateDate;
	private String undertake;
	private String qualificationFile;
	private Set<Attachment> attachmentSet = new TreeSet<Attachment>(new Comparator<Attachment>() {
		@Override
		public int compare(Attachment attFirst, Attachment attSecond) {
			// TODO Auto-generated method stub
			return attFirst.getCreateDate() .compareTo(attSecond.getCreateDate());
		}
	});                        //附件
	private String remark;

	@ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
	@JoinColumn(name = "ORGID")
	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	

	@Column(name = "GRADE")
	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	@Column(name = "CODE")
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@Column(name = "CERTIFICATE")
	public String getCertificate() {
		return certificate;
	}

	public void setCertificate(String certificate) {
		this.certificate = certificate;
	}

	@Column(name = "CERTIFICATEDATE")
	@Temporal(TemporalType.DATE)
	public Date getCertificateDate() {
		return certificateDate;
	}

	public void setCertificateDate(Date certificateDate) {
		this.certificateDate = certificateDate;
	}

	@Column(name = "UNDERTAKE")
	public String getUndertake() {
		return undertake;
	}

	public void setUndertake(String undertake) {
		this.undertake = undertake;
	}

	@Column(name = "QUALIFICATIONFILE")
	public String getQualificationFile() {
		return qualificationFile;
	}

	public void setQualificationFile(String qualificationFile) {
		this.qualificationFile = qualificationFile;
	}

	@Column(name = "REMARK")
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	
	@OneToMany(cascade=CascadeType.PERSIST,fetch=FetchType.LAZY)
	@JoinColumn(name = "TABLEID")
	@Where(clause="delFlag = 0")
	public Set<Attachment> getAttachmentSet() {
		return attachmentSet;
	}
	
	public void setAttachmentSet(Set<Attachment> attachmentSet) {
		
		this.attachmentSet = attachmentSet;
	}
	
	@Transient
	public Attachment getAttachment(){
		if(attachmentSet.iterator().hasNext())
		return attachmentSet.iterator().next();
		return new Attachment();
	}

	public void setAttachment(Attachment attachment){
		attachmentSet.clear();
		attachmentSet.add(attachment);
	}
	public Qualification() {
		super();
	}

}
